//
//  AELDrivingTestController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/30.
//

import UIKit
import WMZDialog
class AELDrivingTestController: AELBaseViewController {
   
   @IBOutlet var tableView: UITableView!
    lazy var viewModel: AELDrivingTestViewModel = {
        return AELDrivingTestViewModel()
    }() 
    var searchStr = ""
    @IBOutlet var searchTF: UITextField!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.searchStr = ""
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        setTab()
        // Do any additional setup after loading the view.
    }
    @IBAction func tapSearchAction(_ sender: Any) {
        self.view.endEditing(true)
        self.searchStr = self.searchTF.text!
        self.loadData()
    }
    @IBAction func newAnniversaryAction(_ sender: Any) {
        self.navigationController?.pushViewController(AELAddDrivingTestController(), animated: true)
    }
    
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "AELDrivingTestTrueOrFalseCell", bundle: .main), forCellReuseIdentifier: "AELDrivingTestTrueOrFalseCell")
        self.tableView.register(UINib(nibName: "AELDrivingTestChoiceCell", bundle: .main), forCellReuseIdentifier: "AELDrivingTestChoiceCell")
    }
    func loadData(){
        self.viewModel.getPageData(self.searchStr) { models in
            print(self.viewModel.lately)
            self.tableView.reloadData()
        }
        
    }
}
extension AELDrivingTestController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = self.viewModel.dataSource[indexPath.row]

        if model.type == 1 {
            let cell:AELDrivingTestChoiceCell = tableView.dequeueReusableCell(withIdentifier: "AELDrivingTestChoiceCell") as! AELDrivingTestChoiceCell
            cell.model = model
            return cell
        }else{
            let cell:AELDrivingTestTrueOrFalseCell = tableView.dequeueReusableCell(withIdentifier: "AELDrivingTestTrueOrFalseCell") as! AELDrivingTestTrueOrFalseCell
            cell.model = model
            return cell
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
